package freespiritsocial.io.user.ui.adapter

import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import freespiritsocial.io.user.R
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.databinding.ItemPostBinding
import freespiritsocial.io.user.ext.circleImageUrl
import freespiritsocial.io.user.ext.loadImageWithCorner
import freespiritsocial.io.user.ext.setAdapterAnimation
import java.util.*

/**
 * 首页帖子适配器
 */
class PostAdapter(
    data: ArrayList<PostPojo> = arrayListOf(),
    val userSelf: Boolean = false,
    layoutResIdRes: Int = R.layout.item_post
):
    BaseQuickAdapter<PostPojo,BaseDataBindingHolder<ItemPostBinding>>(layoutResIdRes,data) {

    init {
        setAdapterAnimation(0)
    }

    override fun convert(holder: BaseDataBindingHolder<ItemPostBinding>,item: PostPojo) = with(holder.dataBinding) {
        this?.iconPostSaveFlag?.isVisible = false
        this?.ivBackground?.loadImageWithCorner(item.coverUrl,14,default = if (item.coverUrl.isNullOrEmpty()) -1 else R.drawable.hodler)
        this?.ivSponsored?.circleImageUrl(item.userAvatar)
        this?.iconPostSaveFlag?.isVisible = item.saveFlag
        this?.groupSponsored?.isVisible = item.advertisersId != null && userSelf.not()
        this?.tvSponsoredName?.text = item.userName
        this?.tvLike?.text = item.likeCountStr
        this?.tvComment?.text = item.commentCountStr
        this?.tvAddress?.text = item.locationName
        this?.userSelf = this@PostAdapter.userSelf
    }
}